/**
 *
 */
package com.ssyt.examinationsystem.dao.impl;

import com.jdbc.util.JdbcUtils;
import com.ssyt.examinationsystem.dao.IRoleInfoDAO;
import com.ssyt.examinationsystem.entity.RoleInfo;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
public class RoleInfoDAOImpl implements IRoleInfoDAO {

    /**
     * 查询所有角色的实现方法
     */
    public List<Map<String, Object>> getRoleInfos() {
        return JdbcUtils.executeQuery("select * from roleinfo");
    }

    @Override
    public int addRoleInfo(RoleInfo role) {
        String sql = "insert into roleInfo values(null,?)";
        return JdbcUtils.executeUpdate(sql, role.getRoleName());
    }

    @Override
    public int deleteRoleInfo(String roleId) {
        String sql = "delete from roleInfo where roleId=?";
        return JdbcUtils.executeUpdate(sql, roleId);
    }

    @Override
    public Map<String, Object> getRoleByRoleId(String roleId) {
        String sql = "select * from roleinfo where roleId=?";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, roleId);
        return CollectionUtils.isEmptyForList(list) ? list.get(0) : null;
    }

    @Override
    public int updateRoleInfo(RoleInfo role) {
        String sql = "update roleinfo set rolename=? where roleid=?";
        Object[] parameters = {role.getRoleName(), role.getRoleId()};

        return JdbcUtils.executeUpdate(sql, parameters);
    }

}
